import os
import logging
from flask import Flask
from flask_cors import CORS

# from flask_migrate import Migrate

from ExtendRegister.conf_register import register_config  # 配置
from ExtendRegister.bp_register import register_bp  # 蓝图


# from ExtendRegister.command_register import register_commands  # commands
from ExtendRegister.excep_register import register_excep
from ExtendRegister.hook_register import register_hook  # 拦截器注册
from ExtendRegister.db_register import register_db, db  # db
from ExtendRegister.model_register import *  # models

cwd_dir = os.getcwd()
static_dir = "\\".join([os.getcwd(), "frontend", "dist"])
template_dir = os.path.join(cwd_dir, "templates")

LOGGER = logging.getLogger(__name__)


def create_app():
    """应用实例"""

    # print(f"cwd_dir:{cwd_dir}")
    # print(f"static_dir:{static_dir}")
    # print(f"template_dir:{template_dir}")
    app = Flask(__name__, static_folder=static_dir, static_url_path="")  # 实例
    app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 1  # disable caching
    CORS(app, supports_credentials=True)  # 跨域
    # register_commands(app)  # flask cli 注册

    register_excep(app)
    register_config(app)  # 配置注册
    register_hook(app)  # 拦截器注册(需要在蓝图之前)
    register_bp(app)  # 蓝图注册
    register_db(app)  # db注册
    # Migrate(app, db)  # ORM迁移

    LOGGER.info("初始化 flask_APP 完成")

    return app
